package com.z.jdbc_demo;
import java.sql.*;
public class JdbcExample4 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.获取数据库连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zk?user=root&password=123456");
        // 3.创建Statement
        // 通过 ? 占位符来设置值
        PreparedStatement preparedStatement = connection.prepareStatement("insert into t_student(stu_name, stu_score, stu_age) values(?, ? ,?)");
        // 为占位符设置具体的值，索引从1开始，执行sql语句，获取结果
        preparedStatement.setString(1, "Alice");
        preparedStatement.setDouble(2, 66.66);
        preparedStatement.setInt(3, 18);
        // 4.执行编写的SQL，获取结果
        preparedStatement.executeUpdate();
        // 5.处理结果
        if (preparedStatement.getUpdateCount() != 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        // 6.释放资源
        preparedStatement.close();
        connection.close();
    }
}
